package org.example;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseFilterUser {
    public static void main(String[] args) {
        try {

            Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "ljc102,ljc103,ljc104");
            conf.set("hbase.zookeeper.property.clientPort", "2181"); // 默认端口是2181


            Connection connection = ConnectionFactory.createConnection(conf);


            Table userTable = connection.getTable(TableName.valueOf("lzb0117_users"));

            Scan scan = new Scan();
            SingleColumnValueFilter filter = new SingleColumnValueFilter(
                    Bytes.toBytes("info"),
                    Bytes.toBytes("name"),
                    CompareOp.EQUAL,
                    new SubstringComparator("李")
            );
            filter.setFilterIfMissing(true);
            scan.setFilter(filter);
            ResultScanner scanner = userTable.getScanner(scan);

            System.out.println("列值中包含‘李’的用户数据：");
            for (Result result : scanner) {
                byte[] row = result.getRow();
                byte[] name = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));
                byte[] email = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("email"));

                System.out.println("Row: " + Bytes.toString(row));
                System.out.println("Name: " + Bytes.toString(name));
                System.out.println("Email: " + Bytes.toString(email));
                System.out.println("---------------");
            }
            scanner.close();


            userTable.close();


            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
